package com.lxb.blog.web.admin;

import com.lxb.blog.service.AdminCommentService;
import com.lxb.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


@Controller
@RequestMapping("/admin")
public class CommentsController {

    @Autowired
    private AdminCommentService adminCommentService;

    @GetMapping("/comments")
    public String comment(@PageableDefault(size = 4,sort = {"createTime"},direction = Sort.Direction.DESC)
                                  Pageable pageable,
                          Model model){
        model.addAttribute("page",adminCommentService.listComments(pageable));
        return "admin/comments";
    }

    @GetMapping("/comments/{id}/delete")
    public String delete(@PathVariable Integer id, RedirectAttributes attributes){

        try {
            adminCommentService.delete(id);
       }catch (Exception e){
            attributes.addFlashAttribute("message","该评论有子评论，不能直接删除");
            return "redirect:/admin/comments";
       }
        attributes.addFlashAttribute("message","删除成功！");
        return "redirect:/admin/comments";



    }
}
